$(function () {
    "use strict";

    /* get user infomation */
    $.ajax({
        url: 'PersonalPage/getuserinfo',
        type: 'post',
        success: function (reasult) {
            var avatar = '';
            var info = '';
            var btn = '';
            $.each(reasult.data, function (key, value) {
                $('.personal-bg').css('background-image', 'url("' + value.bg + '")');
                avatar = '<a href="' + value.username + '" class="personal-avtarLink"><img class="personal-avatarImage size175" src="' + value.avatar + '"/></a>';
                info = '<h1><a href = "' + value.username + '">' + value.nickname + '</a></h1><h2><a href="' + value.username + '">@<span>' + value.username + '</span></a></h2><p>' + value.personal + '</p> ';
                if ($('#sessionid').val() == value.id) {
                    $('#personal-nav-btn').css('padding', '12px 0 0 2px');
                    btn = '<a href="Settings" class="personal-nav-btn1">编辑个人资料</a>';
                } else {
                    btn = '<button type="button" class="personal-nav-btn"><span class="button-text follow-text"> 关注 </span><span class="button-text following-text">正在关注</span><span class="button-text unfollow-text">取消关注</span></button>';
                }
                setfollow();

                $('.personal-avatar').html(avatar);
                $('.personal-info').html(info);
                $('#personal-nav-btn').html(btn);
            });
        }
    });
    getmood();
    $('#mood').on('click', function () {
        $('#pic').removeClass('isonclick');
        getmood();
    });
    $('#pic').on('click', function () {
        $('#mood').removeClass('isonclick');
        getpicmood();
    });
    /* getmood */
    function getmood() {
        $.ajax({
            url: 'Getmood/getpersonalmood',
            type: 'post',
            dataType: 'json',
            error: function () {

            },
            success: function (result) {
                $('#mood').addClass('isonclick');
                drawmood(result);
            }
        });
    }

    function getpicmood() {
        $.ajax({
            url: 'Getmood/getpicmood',
            type: 'post',
            dataType: 'json',
            error: function () {

            },
            success: function (result) {
                $('#pic').addClass('isonclick');
                drawmood(result);
            }
        });
    }

    /*getusersinfomation*/
    function getusersinfo(follow) {
        if (follow == 1) {
            $.ajax({
                url: 'PersonalPage/getfollow',
                type: 'post',
                data: {
                    'type': 'following'
                },
                error: function () {

                },
                success: function (result) {
                    drawbox(result);
                }
            });
        }
        if (follow == 2) {
            $.ajax({
                url: 'PersonalPage/getfollow',
                type: 'post',
                data: {
                    'type': 'followed'
                },
                error: function () {

                },
                success: function (result) {
                    drawbox(result);
                }
            });
        }
    }


    function drawmood(result) {
        $('#weibo').css('display','block');
        $('#follow').css('display','none');
        var html ='';
        $.each(result.data, function (key, value) {
            html += '<li class="stream-item" id="' + value.id + '"><div class="stream-item-detail"><div class="stream-item-content"><div class="stream-item-header"><a class="account-group" href="' + value.username + '"><img class="stream-item-avater size48" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><a class="timestamp"><span class="_timestamp">' + value.datetime + '</span></a></small></div><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p></div><div class="stream-pic-container"><div class="stream-pic-view">';
            if (value.photo) {
                html += '<img src="' + value.photo + '">';
            }
            html += '</div></div><div class="stream-item-footer"><div class="stream-item-action-list"><div class="stream-item-action action-huifu"><button class="stream-item-action-button" name="' + value.id + '" type="button"><span class="iconfont icon-xiaoxi iconfont-normal"></span><span class="icontextcount "><span class="person-count" id="huifu-count">' + value.hfnum + '</span></span><input type="hidden" value="1"></button></div><div class="stream-item-action"><button class="stream-item-action-button action-zhuanfa" name="' + value.id + '" type="button"><span class="iconfont icon-fenxiang iconfont-normal"></span><span class="icontextcount "><span class="person-count" id="zhuanfa-count">' + value.fowardnum + '</span></span></button></div><div class="stream-item-action"><button class="stream-item-action-button action-dianzan" name="' + value.id + '" type="button"><span class="iconfont icon-shoucang iconfont-normal"></span><span class="icontextcount "><span class="person-count" id="dianzan-count">' + value.shoucang + '</span></span></button></div><div class="stream-item-action"><button class="stream-item-action-button action-transhbin" name="' + value.id + '" type="button"><span class="iconfont icon-trashbin iconfont-normal"></span></button></div></div></div></div></div><div id="repeat-item" class="stream-item-repeat" style="display: none;"><div class="list-repeat"></div><div class="list-repeats"><ul class="list-box"></ul></div></div></div></li>';
        });
        $('.personal_r #weibo #stream-items-id').html(html);
        $('#stream-items-id .action-huifu').delegate('button', 'click', function (event) {
            event.stopPropagation();
            event.preventDefault();

            var flag = $(this).find(':input:eq(0)');
            var t = $(this).parent().parent().parent().parent().parent().parent();
            if (flag.val() == 1) {
                flag.attr('value', '0');
                t.find('.stream-item-repeat').show();
                var s = $('.me-btn img')[0].src;
                var h = t.find('.username').find('b').text();
                /*repeat 头*/
                var html = '<div class="repeat"><div class="repeat-avatar size32"><img class="size32" src="' + s + '"></div><div class="publish"><div class="p-input"><textarea class="repeat-textarea" name="content" style="margin: 0;border-style: none;border-width: 0;font-size: 14px;word-wrap:break-word;line-height: 18px;overflow-y: auto;overflow-x: hidden;outline: none;"></textarea></div><div class="p-send"><div class="send-box-bar"><button id="repeat-btn" class="send-box-btn .send-repeat" type="button" disabled="disabled">评论</button></div></div></div></div>';
                t.find(".list-repeat").html(html);
                /*send repeat*/
                //判断文本框字数
                t.find(".repeat-textarea").on("input", function () {
                    var hftext = $(this).val();
                    var s = new RegExp(/^\s+$/);
                    var num = $(this).val().length;
                    if (num === 0 || num > 140) {
                        t.find("#repeat-btn").attr("disabled", "disabled");
                    } else {
                        if (s.test(hftext)) {
                            t.find("#repeat-btn").attr("disabled", "disabled");
                        } else {
                            t.find("#repeat-btn").removeAttr("disabled");
                        }
                    }
                });
                t.find("#repeat-btn").on('click', function () {
                    var content = t.find(".repeat-textarea").val();
                    var tocomment = t.attr('id');
                    $.ajax({
                        url: 'Repeat/sendrepeat',
                        type: 'post',
                        data: {
                            content: content,
                            tocomment: tocomment,
                            towho: h
                        },
                        success: function (result) {
                            t.find(".action-huifu #huifu-count").html(result);
                            t.find(".repeat-textarea").val("");
                            $.ajax({
                                url: 'Repeat/getrepeat',
                                type: 'post',
                                data: {
                                    id: t.attr('id')
                                },
                                success: function (result) {
                                    var html = '';
                                    var hfnum = '';
                                    $.each(result.data, function (key, value) {
                                        hfnum = value.hfnum;
                                        html += '<li class="list-li" id="' + value.id + '"><a id="' + value.who + '" class="account-group" href="' + value.username + '"><img class="stream-item-avater size32" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><span class="timestamp"><span class="_timestamp">' + value.datetime + '</span></span></span></small><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p></div><div class="p-send"><div class="send-box-bar"><button id="repeat-btn2" class="send-box-btn repeat-btn2" type="button">回复</button></div></div></div></li>';
                                    });
                                    t.find('.list-box').html(html);
                                    t.find(".action-huifu #huifu-count").html(hfnum);
                                    t.find('.list-box li').first().css("border-top", "1px solid #d9d9d9");
                                    t.find('.list-box li').last().css("border", "none");
                                    sendrepeat();
                                }
                            });
                            t.find("#repeat-btn").attr("disabled", "disabled");
                        }
                    });
                });
                $.ajax({
                    url: 'Repeat/getrepeat',
                    type: 'post',
                    data: {
                        id: t.attr('id')
                    },
                    success: function (result) {
                        var html = '';
                        var hfnum = '';
                        $.each(result.data, function (key, value) {
                            hfnum = value.hfnum;
                            html += '<li class="list-li" id="' + value.id + '"><a id="' + value.who + '" class="account-group" href="' + value.username + '"><img class="stream-item-avater size32" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><span class="timestamp"><span class="_timestamp">' + value.datetime + '</span></span></span></small><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p></div><div class="p-send"><div class="send-box-bar"><button id="repeat-btn2" class="send-box-btn repeat-btn2" type="button">回复</button></div></div></div></li>';
                        });
                        t.find('.list-box').html(html);
                        t.find(".action-huifu #huifu-count").html(hfnum);
                        t.find('.list-box li').first().css("border-top", "1px solid #d9d9d9");
                        t.find('.list-box li').last().css("border", "none");
                        sendrepeat();
                    }
                });

            } else {
                flag.attr('value', '1');
                t.find('.stream-item-repeat').hide();
            }
            function sendrepeat() {
                t.find(".repeat-btn2").on('click', function () {
                    var th = $(this).parent().parent().parent();
                    var thid = th.attr('id');
                    $('#global-repeat-box').find('.modal-title').empty();
                    $('.modal-repeat').empty();
                    $.ajax({
                        url: 'Repeat/getwhorepeat',
                        type: 'post',
                        data: {
                            towho: thid
                        }, success: function (result) {
                            var html = '';
                            var h3 = '';
                            $.each(result.data, function (key, value) {
                                html = '<div class="list-li" id="' + value.who + '"><a class="account-group" href="' + value.username + '"><img class="stream-item-avater size48" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><span class="timestamp"><span class="_timestamp">' + value.datetime + '</span></span></span></small><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p></div>';
                                h3 = '回复@' + value.username + '';
                            });
                            $('#global-repeat-box').find('.modal-title').html(h3);
                            h3 += '：';
                            $('#global-repeat-box').find('.send-box-textarea').html(h3);
                            $('.modal-repeat').html(html);
                        }
                    });
                    $('#global-repeat-box').css("display", "block");
                    $('.global-dialog').show();
                    $('#global-repeat-box').find('.send-box-textarea').scrollTop();

                    $("#global-repeat-box .send-box-textarea").on("input", function () {
                        var towho = $('#global-repeat-box').find('.modal-title').text();
                        towho += '：';
                        var hftext = $(this).val();
                        var s = new RegExp(/^\s+$/);
                        var num = $(this).val().length;
                        $('#global-repeat-box .counter').html(140 - num);

                        if (num === 0 || num > 140) {
                            $("#global-repeat-box .send-box-btn").attr("disabled", "disabled");
                        } else {
                            if (s.test(hftext)) {
                                $("#global-repeat-box .send-box-btn").attr("disabled", "disabled");

                            } else if (towho == hftext) {
                                $("#global-repeat-box .send-box-btn").attr("disabled", "disabled");
                            } else {
                                $("#global-repeat-box .send-box-btn").removeAttr("disabled");
                            }
                        }
                    });
                    $("#global-repeat-box .send-repeat").off().on('click', function () {
                        $("#global-repeat-box .send-box-btn").attr("disabled", "disabled");
                        var content = $('#global-repeat-box .send-box-textarea').val();
                        var tocomment = t.attr('id');
                        var towho = $('#global-repeat-box').find('.username').find('b').text();
                        $.ajax({
                            url: 'Repeat/sendrepeat',
                            type: 'post',
                            data: {
                                content: content,
                                tocomment: tocomment,
                                towho: towho
                            },
                            success: function (result) {
                                t.find(".action-huifu #huifu-count").html(result);
                                t.find(".repeat-textarea").val("");
                                $('#global-repeat-box .send-box-textarea').val("");
                                $.ajax({
                                    url: 'Repeat/getrepeat',
                                    type: 'post',
                                    data: {
                                        id: t.attr('id')
                                    },
                                    success: function (result) {
                                        var html = '';
                                        var hfnum = '';
                                        $.each(result.data, function (key, value) {
                                            hfnum = value.hfnum;
                                            html += '<li class="list-li"  id="' + value.id + '"><a id="' + value.who + '" class="account-group" href="' + value.username + '"><img class="stream-item-avater size32" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><span class="timestamp"><span class="_timestamp">' + value.datetime + '</span></span></span></small><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p></div><div class="p-send"><div class="send-box-bar"><button id="repeat-btn2" class="send-box-btn" type="button">回复</button></div></div></div><div class="repeat-list"></div></li>';
                                        });
                                        t.find('.list-box').html(html);

                                        t.find('.list-box li').first().css("border-top", "1px solid #d9d9d9");
                                        t.find('.list-box li').last().css("border", "none");
                                    }
                                });
                            }
                        });
                        $('#global-repeat-box').css("display", "none");
                        $(".global-dialog").hide();
                    });
                });
            }
        });
    }

    function drawbox(result) {
        $('#follow').css('display','block');
        $('#weibo').css('display','none');
        var html = '';
        $.each(result.data, function (key, value) {
            html += '<div class="info-box"><div class="info-box-item" id="' + value.id + '"><a class="info-box-item-bg" href="' + value.username + '"><img src="' + value.bg + '"></a><div class="info-box-item-content"><a class="info-box-a size75" href="' + value.username + '"><img class="size75" src="' + value.avatar + '"></a><span class="screenname"><a href="' + value.username + '">@<span>' + value.username + '</span></a></span><p class="info-box-bio">' + value.personal + '</p><div class="person-info-atten"><ul class="personal-nav-list"><li><a href="'+value.username+'" class="p-txt1" style="padding-left: 10px;"><span class="p-txt2">微博</span><strong>'+value.moodnum+'</strong></a></li><li><a href="'+value.username+'" class="p-txt1" style="padding-left: 0;"><span class="p-txt2">正在关注</span><strong>'+value.following+'</strong></a></li><li><a href="'+value.username+'" class="p-txt1" style="padding-left: 0;"><span class="p-txt2">关注者</span><strong>'+value.followed+'</strong></a></li></ul></div></div></div></div>';
        });
        $('.personal_r #follow').html(html);
    }


    /*set follow*/
    function setfollow() {
        $.ajax({
            url: 'Followsetting/getuserfollow',
            type: 'post',
            success: function (result) {
                if (result == 1) {
                    following();
                } else {
                    mousehover();
                }
            }
        });
        $('#personal-nav-btn').delegate('button', 'click', function (e) {
            $.ajax({
                url: 'Followsetting/follow',
                type: 'post',
                success: function (result) {
                    if (result == 1) {
                        mousehover();
                        window.location.reload();
                    } else {
                        following();
                        window.location.reload();
                    }
                }
            });
        });

        function following() {
            $('.follow-text').css('display', 'none');
            $('.unfollow').css('display', 'none');
            $('.following-text').css('display', 'block');
            $('.personal-nav-btn').addClass('following');
            $('#personal-nav-btn').delegate('button', 'mouseenter mouseleave', function (e) {
                if (e.type == 'mouseenter') {
                    var f = $('.following-text').css('display');
                    if (f == 'block') {
                        $('.following-text').css('display', 'none');
                        $('.unfollow-text').css('display', 'block');
                        $('.personal-nav-btn').removeClass('following');
                        $('.personal-nav-btn').addClass('unfollow');
                    }
                }
                if (e.type == 'mouseleave') {
                    $('.following-text').css('display', 'block');
                    $('.unfollow-text').css('display', 'none');
                    $('.personal-nav-btn').removeClass('unfollow');
                    $('.personal-nav-btn').addClass('following');
                }
            });
        }

        function mousehover() {
            $('.unfollow').css('display', 'none');
            $('.following-text').css('display', 'none');
            $('.follow-text').css('display', 'block');
            $('#personal-nav-btn').delegate('button', 'mouseenter mouseleave', function (e) {
                if (e.type == 'mouseenter') {
                    $('.personal-nav-btn').addClass('follow');
                }
                if (e.type == 'mouseleave') {
                    $('.personal-nav-btn').removeClass('follow');
                }
            });
        }
    }


    $('.personal-nav-list li').on('click', function () {
        var list = $(this);
        var t = list.index();
        $('.personal-nav-list li').find('.personal-nav-start').removeClass('isactive');
        list.find('.personal-nav-start').addClass('isactive');
        switch (t) {
            case 0:
                getmood();
                break;
            case 1:
                getusersinfo(1);
                break;
            case 2:
                getusersinfo(2);
                break;
        }
    });
});